<script setup>
import {ref} from 'vue'
import {categoriesListService} from "@/api/categories.js";
import router from "@/router/index.js";

const totalpage = ref(0)
const categoriesList = ref([])
const queryConfig = ref({
  type: 3,
  pagenum: 1,
  pagesize: 10
})
const getInit = async () => {
  const res = await categoriesListService(queryConfig.value)
  categoriesList.value = res.result
  totalpage.value = res.total
}

getInit()
</script>
<template>
  <el-card>
    <template #header>
      <div class="header">
        <h2>商品分类列表</h2>
        <el-button type="primary" @click="">添加分类</el-button>
      </div>
    </template>

    <el-checkbox-group v-model="queryConfig.type">
      <el-checkbox-button value="1">一级分类</el-checkbox-button>
      <el-checkbox-button value="2">二级分类</el-checkbox-button>
      <el-checkbox-button value="3">三级分类</el-checkbox-button>
    </el-checkbox-group>
    <el-table :data="categoriesList">
      <el-table-column type="index" label="序号" width="80"/>
      <el-table-column prop="cat_name" label="分类名称"></el-table-column>
      <el-table-column prop="cat_pid" label="父级分类">
        <template #default="{row}">
          {{ row.cat_pid === 0 ? '最高级分类' : row.cat_pid }}
        </template>
      </el-table-column>
      <el-table-column prop="cat_level" label="分类层级">
        <template #default="{row}">
          <el-tag v-if="row.cat_level === 0" type="success">一级分类</el-tag>
          <el-tag v-else-if="row.cat_level === 1" type="success">二级分类</el-tag>
          <el-tag v-else type="success">三级分类</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button type="primary">编辑</el-button>
          <el-button type="success" @click="router.push({path:`/params/${row.cat_id}`})">修改参数</el-button>
          <el-button type="danger">删除</el-button>
        </template>
      </el-table-column>
      <template #default="{row}" v-if="chrildren" prop="children" label="子分类">
        <el-table :data="row.children">
          <el-table-column type="index" label="序号" width="80"/>
          <el-table-column prop="cat_name" label="分类名称"/>
        </el-table>
      </template>
    </el-table>
    <div class="page">
      <el-pagination layout="total, sizes, prev, pager, next, jumper"
                     :page-sizes="[10, 15, 20,25,30]"
                     :total="totalpage"
                     :page-size="queryConfig.pagesize"
                     :current-page="queryConfig.pagenum"
                     @update:pageSize="(val)=>{queryConfig.pagesize=val;getInit()}"
                     @update:currentPage="(val)=>{queryConfig.pagenum=val;getInit()}"
                     @prevClick="getInit"
                     @nextClick="getInit"/>
    </div>
  </el-card>
</template>

<style lang="scss" scoped>
.el-card {
  .header {
    display: flex;
    justify-content: space-between;
  }

  .el-table {
  }

  .page {
    display: flex;
    justify-content: center;

    .el-pagination {
      margin: 10px;
    }
  }
}
</style>
